package bbs.controller;


import bbs.annotation.GlobalInterceptor;
import bbs.annotation.VerifyParam;
import bbs.controller.base.ABaseController;
import bbs.entity.config.AdminConfig;
import bbs.entity.constants.Constants;
import bbs.entity.dto.*;
import bbs.entity.enums.ResponseCodeEnum;
import bbs.entity.enums.VerifyRegexEnum;
import bbs.entity.vo.ResponseVO;
import bbs.exception.BusinessException;
import bbs.service.EmailCodeService;
import bbs.service.UserInfoService;
import bbs.utils.StringTools;
import bbs.utils.SysCacheUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;


@RestController("accountController")
public class AccountController extends ABaseController {

    @Resource
    private AdminConfig adminConfig;

    /**
     * 生成图片验证码
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping({"/checkCode"})
    public void checkCode(HttpServletResponse response, HttpSession session) throws IOException {
        CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0L);
        response.setContentType("image/jpeg");
        String str = vCode.getCode();
        session.setAttribute(Constants.CHECK_CODE_KEY, str);
        vCode.write((OutputStream)response.getOutputStream());
    }

    /**
     * 用户登录
     * @param session
     * @param account
     * @param password
     * @param checkCode
     * @return
     */
    @RequestMapping({"/login"})
    public ResponseVO login(HttpSession session,
                               @VerifyParam(required = true) String account,
                               @VerifyParam(required = true) String password,
                               @VerifyParam(required = true) String checkCode)
    {
        try {
            if (!checkCode.equalsIgnoreCase((String) session.getAttribute(Constants.CHECK_CODE_KEY))) {
                throw new BusinessException(Constants.IMAGE_CODE_ERROR);
            }
            if (!adminConfig.getAdminAccount().equals(account) || StringTools.encodeMd5(adminConfig.getAdminPassword()).equals(StringTools.encodeMd5(password))) {
                throw new BusinessException(Constants.EMAIL_PASSWORD_ERROR);
            }

            SessionAdminUserDto sessionAdminUserDto = new SessionAdminUserDto();
            sessionAdminUserDto.setAccount(account);
            session.setAttribute(Constants.SESSION_KEY, sessionAdminUserDto);
            return getSuccessResponseVO(sessionAdminUserDto);
        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }

    /**
     * 登出
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    @GlobalInterceptor()
    public ResponseVO logout(HttpSession session) {
        session.invalidate();
        return getSuccessResponseVO(null);
    }

}
